//
//  PACurrentChartVC.m
//  PilotSeries
//
//  Created by 黄立强 on 16/6/30.
//  Copyright © 2016年 leon.huang. All rights reserved.
//

#import "PACurrentChartVC.h"
#import "PAAppContext.h"
#import "PAChartConfigManager.h"
#import "ChartConfig.h"
#import "PAAccountManager.h"
#import "PAGroupManager.h"
#import "PARangeListVC.h"
#import "PAChartListVC.h"
#import "PACurrentChartCell.h"
#import "PACreateDirectionConfigVC.h"

@interface PACurrentChartVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@end

@implementation PACurrentChartVC

#pragma mark - 懒加载
- (NSMutableArray *)currentCharts
{
    if (_currentCharts == nil) {
        _currentCharts = [[NSMutableArray alloc] init];
    }
    return _currentCharts;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUp];
 
}

-(void)setUp{
    self.tableView.backgroundColor = PAGlobalBg;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
  
    UIBarButtonItem *addChart  =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createChart:)];
    
     UIBarButtonItem *createChart  =  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(addChart:)];
    // 设置导航栏左边的按钮
    self.navigationItem.rightBarButtonItems =@[addChart , createChart];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self.currentCharts removeAllObjects];
    [self.currentCharts addObjectsFromArray:[PAChartConfigManager loadCurrentChartConfigs]];
    [self.tableView reloadData];
    [self.tableView reloadEmptyDataSet];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.currentCharts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PACurrentChartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"current_chart" forIndexPath:indexPath];
    ChartConfig *chartConfig = (ChartConfig *)self.currentCharts[indexPath.row];
    cell.titleLabel.text = chartConfig.chartName;
    
    return cell;
}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"placeholder_kickstarter"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"No chart setting";
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0],
                                 NSForegroundColorAttributeName: [UIColor colorWithRed:170/255.0 green:171/255.0 blue:179/255.0 alpha:1.0],
                                 NSParagraphStyleAttributeName: paragraphStyle};
    
    return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"Please tap the search/plus button to add/create chart";
    
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.0],
                                 NSForegroundColorAttributeName: [UIColor colorWithRed:170/255.0 green:171/255.0 blue:179/255.0 alpha:1.0],
                                 NSParagraphStyleAttributeName: paragraphStyle};
    
    return [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
}
// 只要实现这个方法，就会有滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ChartConfig *config = self.currentCharts[indexPath.row];
    // 删除模型
    [self.currentCharts removeObjectAtIndex:indexPath.row];
    //删除数据
    [PAChartConfigManager deleteCurrentChartConfigByChartName:config.chartName];
     NSString *groupName = [PAAccountManager loadCurrentGroupName];
    //判断当前所在的group,同步删除group中的chart
    if (groupName!=nil) {
        [PAGroupManager updateGroup:groupName withChartNames:[self.currentCharts valueForKey:@"chartName"]];
    }
    
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //通知Dock更新
    [[NSNotificationCenter defaultCenter] postNotificationName:PACurrentChartDeleteNotification object:nil userInfo:@{@"index":@(indexPath.row)}];
    self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (self.currentCharts.count>0) {
        return @"CURRENT CHARTS DISPLAY IN DOCK VIEW";
    }
    return @"";
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (self.currentCharts.count>0) {
        return @"SWIPE LEFT TO DELETE CHART";
    }
    return @"";
}

- (BOOL)checkDockSize
{
    NSInteger size = [PAAccountManager loadCurrentDockSize];
    NSArray *chartConfigs =  [PAChartConfigManager loadCurrentChartConfigs];
    if (size<=chartConfigs.count) {
        [CRToastManager showNotificationWithMessage:@"already more than limit chart size!please change dockSize first!" completionBlock:^{
            
        }];
        return NO;
    }
    return YES;
}

-(void)addChart:(UIBarButtonItem *)buttonItem
{
    
    if (![self checkDockSize]) return;
    PAChartListVC *chartListVC = [PAChartListVC new];
    [self.navigationController pushViewController:chartListVC animated:YES];
    
}

-(void)createChart:(UIBarButtonItem *)buttonItem
{
   
    if (![self checkDockSize]) return;
//    PARangeListVC *rangeListVC = [PARangeListVC new];
    //[self.navigationController pushViewController:rangeListVC animated:YES];
    PACreateDirectionConfigVC *dv = [[PACreateDirectionConfigVC alloc] init];
    [self.navigationController pushViewController:dv animated:YES];
}
@end
